package com.lagou.edu.service.impl;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.Pojo.HotPosition;
import com.lagou.edu.dao.GuavaTestDao;
import com.lagou.edu.service.GuavaCacheService;
import com.lagou.edu.service.GuavaRedisService;
import com.lagou.edu.service.GuavaTestService;
import org.omg.CORBA.SetOverrideType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class GuavaTestServiceImpl implements GuavaTestService{


	@Autowired
	private GuavaCacheService guavaCacheService;

	@Autowired
	private GuavaTestDao guavaTestDao;

	@Autowired
	private GuavaRedisService guavaRedisService;

	@Override
	public List<HotPosition> getSourceFromGuavacache() {
		System.out.println("===============================");
		System.out.println("如果guavacache有数据直接返回================");
		Object o = guavaCacheService.get("hotPosition");
		if (o != null) {
			System.out.println("guava获取：" + o);
			return JSON.parseArray(JSON.toJSONString(o), HotPosition.class);
		} else {
			System.out.println("guava中没有数据直接从redis中读取数据=============");
			List<HotPosition> hotPositions = getSourceFromredis();
			guavaCacheService.put("hotPosition", hotPositions);
			System.out.println("guava设置：" + hotPositions);
			return hotPositions;
		}
	}

	@Override
	public List<HotPosition> getSourceFromdata() {
		List<HotPosition> hotPositions = guavaTestDao.findAll();
		System.out.println("datasource获取：" + hotPositions);
		return hotPositions;
	}

	@Override
	public List<HotPosition> getSourceFromredis() {
		Object hotPosition = guavaRedisService.get("hotPosition");
		if (hotPosition == null) {
			System.out.println("redis中无法获取数据从数据库读取存入redis中===================");
			List<HotPosition> datasourceWork = getSourceFromdata();
			String s = JSON.toJSONString(datasourceWork);
			guavaRedisService.put("hotPosition", s);
			hotPosition = s;
			System.out.println("redis设置：" + hotPosition);
		} else {
			System.out.println("redis中有数据直接获取==========================");
			System.out.println("redis获取：" + hotPosition);
		}
		return JSON.parseArray(hotPosition.toString(), HotPosition.class);
	}


}
